import sys
import tkinter as tk
from page.StartPage import StartPage
from page.SetPage import SetPage
from page.UsePage import UsePage
from page.Config import Config
import io

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        # 获取屏幕尺寸
        config = self.getScreenSize()

        self.geometry(f"800x600+{int((config.data['screenWidth']-800)/2)}+{int((config.data['screenHeight']-600)/2)}")
        
        # 创建容器Frame
        container = tk.Frame(self)
        container.pack(fill="both", expand=True)

        # 标题
        self.title("抽奖系统")
        
        # 创建多个界面Frame
        self.frames = {}
        self.current_page = None
        for F in (StartPage,SetPage,UsePage): #这里增加页面
            frame = F(container, self)
            self.frames[F] = frame
            if F != UsePage:
                frame.place(relx=0, rely=0, relwidth=1, relheight=1)    #设定页面大小
            else:
                frame.place(relx=0, rely=0, relwidth=1, relheight=1)
                # frame.place(x=0, y=0, width=config.data['screenWidth'], height=config.data['screenHeight'])    
            frame.pack_propagate(True) 
        
        #打开主界面
        self.show_frame(StartPage)

        # 登录状态初始化
        self.setLogin()
            
    #切换页面
    def show_frame(self, cont):
        if self.current_page:
            self.current_page.on_leave()
        self.current_page = self.frames[cont]
        self.current_page.tkraise()
        self.current_page.on_enter()

        frame = self.frames[cont]
        frame.tkraise()  # 将目标Frame提升到最前面

    # 获取屏幕尺寸
    def getScreenSize(self):
        config = Config()
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        config.data['screenWidth'] = screen_width
        config.data['screenHeight'] = screen_height
        config.dump()
        return config

    # 设置登录障碍
    def setLogin(self):
        config = Config()
        config.data['loginStatus'] = False
        config.dump()
        pass

if __name__ == "__main__":
    app = App()
    app.mainloop()
